package com.wnxy.mallserver.controller;

import com.wnxy.mallserver.entity.Address;
import com.wnxy.mallserver.response.ResponseResult;
import com.wnxy.mallserver.service.AddressService;
import com.wnxy.mallserver.vo.PccVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

//@Api(tags = "地址模块的APi文档")
@Slf4j
@RestController
@RequestMapping("/address")
public class AddressController {
    @Resource
    private AddressService addressService;

    /**根据用户id查找地址*/
//    @RequirePerms("user:find")
    @GetMapping("/findArrByUid")
    public ResponseResult<List<Address>> findArrByUid(HttpSession session){
        int uid = (Integer) session.getAttribute("uid");
        return new ResponseResult(ResponseResult.OK,addressService.findArrByUid(uid));
    }

    /**获取省市区的集合*/
//    @RequirePerms("user:find")
    @GetMapping("/getPccList")
    public ResponseResult<List<PccVo>> getPccList(){
        List<PccVo> list = addressService.getPccList();
        return new ResponseResult<>(ResponseResult.OK,list);
    }

    /**
     * 添加地址
     * @Validated:  表明此数据需要校验
     * BindingResult：   用于保存校验未通过的结果.并且要将这个放在需要校验参数的第一顺位，否则会报400
     */
//    @RequirePerms("user:update")
    @PostMapping("/submitAddr")
    public ResponseResult<Integer> submitAddr(@Validated @RequestBody Address address, BindingResult result,
                                              HttpSession session){
//        //判断校验结果是否有不满足条件的数据。有-true，wu-false
//        if(result.hasFieldErrors()){
//            throw new ValidateException(result.getFieldErrors().get(0).getDefaultMessage());
//        }

        int uid = (Integer) session.getAttribute("uid");
        //设置uid和默认地址
        address.setUid(uid);
        address.setDefaultaddr("n");
        return new ResponseResult<>(ResponseResult.OK,addressService.submitAddr(address));//返回id
    }

}
